/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myutil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author Tidus Le
 */
public class XMLHelper
{
    /**
     * Hàm giúp ghi file xml theo định dạng cho trước
     */
    public static void write(String fileName,ArrayList<Hashtable<String,String>> arrElements)
    {
        try
        {
            XMLOutputFactory factory=XMLOutputFactory.newInstance();
            File dir=new File("data");
            if(dir.exists()==false)
                dir.mkdir();
                    
            XMLStreamWriter xmlWriter=factory.createXMLStreamWriter(new FileOutputStream(new File("data\\"+fileName+".xml")),"UTF-8");
            
            xmlWriter.writeStartDocument("UTF-8","1.0"); 
              xmlWriter.writeStartElement("root"); 
              
                for(Hashtable<String,String> elements:arrElements)
                {
                    xmlWriter.writeStartElement("element");
                      Enumeration e=elements.keys();
                      while(e.hasMoreElements())
                      {
                          String key=(String)e.nextElement();
                          xmlWriter.writeStartElement(key);
                            xmlWriter.writeCharacters(elements.get(key));
                          xmlWriter.writeEndElement();
                      }
                    xmlWriter.writeEndElement();
                }
                  
              xmlWriter.writeEndElement(); 
            xmlWriter. writeEndDocument();
            
            xmlWriter.close();
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    
    /**
     * Hàm đọc file XML với tên file cho trước
     */
    public static ArrayList<Hashtable<String,String>> read(String fileName)
    {
        ArrayList<Hashtable<String,String>> arrElements=new ArrayList<Hashtable<String, String>>();
        try
        {
            File dir=new File("data");
            if(dir.exists()==false)
                dir.mkdir();
            
            XMLInputFactory factory=XMLInputFactory.newInstance();
            XMLStreamReader xmlReader=factory.createXMLStreamReader(new FileReader(new File("data\\"+fileName+".xml")));
            Hashtable<String,String> elements=null;
            
            while(xmlReader.hasNext())
            {
                int event=xmlReader.next();
                if(event==XMLStreamConstants.START_ELEMENT)
                {
                    if(xmlReader.getLocalName().equals("element")==true)
                    {
                        elements=new Hashtable<String, String>();
                    }
                    else
                        if(xmlReader.getLocalName().equals("root")==false)
                        {
                            elements.put(xmlReader.getLocalName(), xmlReader.getElementText());
                        }
                }
                else
                    if(event==XMLStreamConstants.END_ELEMENT)
                    {
                        if(xmlReader.getLocalName().equals("element")==true)
                        {
                            arrElements.add(elements);
                        }
                    }
            }
            
            xmlReader.close();
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }finally
        {
            return arrElements;
        }
    }
}
